<template>
  <div :class="appContainerCssClass">
    <div class="filter-container">
      <!--
      a-form-model rules 对动态标签处理时，item-label * 号不出现,需一开始指定，必须绑定,所直接在a-form-model-item标签中直接绑定，也要提取准备好require=true，先设置true，最后再根据规则调整位false
      -->
      <a-form-model ref="ruleForm" :model="formModel" layout="inline">
        <a-row v-for="(rowItem ,rowIndex) in formRowCollapseKey['noCollapseKey']" :key="rowIndex" :gutter="rowItem.gutter">
          <a-col v-for="(formItem,colIndex) in formRowActiveData[rowItem.rowKey].colList" :key="colIndex" :span="formItem.span">
            <input-tpl-ui
              v-show="rowItem.rowType === 'form'"
              ref="modalFormTplUi"
              :param-data="initData"
              :form-item="formColActiveData[formItem.model]"
              :active-enum-data="activeEnumData"
              :active-enum-list="activeEnumData[formItem.model]"
              :modal-fn-data="modalFn"
              :ui-page-url="uiPageUrl"
              :init-src-label="initSrcObj.initSrcLabel"
              :rules="rules"
              :modal-form="formModel"
              :form-event="formEvent"
              :input-style="formItem.inputStyle"
              @changeEvent="inputChangeEvent"
              @clearEmpty="clearEmpty"
            />
          </a-col>
        </a-row>
      </a-form-model>
    </div>
  </div>

</template>

<script>

import {
  changeListColActiveData,
  initListColActive, beforeInitData,
  searchSessionFormData,
  searchSessionTpl
} from '../../../../utils/hylist'

const tableColumns = []
const tableData = []
import { Modal } from 'ant-design-vue'
import Pagination from '@/components/Pagination'
import { submitDealReq, getFnInputParam, getDealParam, processCustomRowColumnBtn, loadSessionUIMethod } from '@/utils/auth'
import { executeRecord, getGlobalParam, deleteLocalList, deleteBatchLocalList, deleteBatchRemoteRecord, deleteRemoteRecord } from '@/utils/hyrecord'
import { initToolbar, changeToolbar, initToolbarMenu, changeToolbarMenu, initColumnListBtn, initRowBtn, initFooterToolbarList } from '@/utils/hybtn'
import { initEnumParam } from '@/utils/hyenum'
import { initColumnList, changeColumnList, initTableEvent, changeTableEvent, fetchPageData, changeSelectRow, initTableRowKey } from '@/utils/hylist'

import { initForm, changeForm, handleInit, initFormFnParam, changeFormFnParam, changeFormKeyFnParam, changeFormRowActiveData } from '@/utils/hyform'
import { initRules, changeFormRules, changeFormKeyRules } from '@/utils/hyformrules'
import { btnUploadFile } from '@/utils/hyupload'
import { getRefOpenObj, getOpenLocalFormModel } from '@/utils/hyopen'
import { initDealApi } from '@/utils/hyurl'
import { inputChangeEventControlObj, getCallFnByFnType, inputChangeEvent, execClickBtn } from '@/utils/hyctr'
import inputTplUi from './inputTplUi'
import operatorBtnTplList from './operatorBtnTplList'
import { I_APP_ID } from '@/settings'
// import columnBtnTplList from './columnBtnTplList'
import request from '@/utils/request'
import vm from 'vm'
import echarts from 'echarts'
import resize from '@/components/Charts/mixins/resize'
import { loadInitDataMethod, loadSessionFormUIDataMethod } from '../../../../utils/auth'
import { getOpenLocalFormList, getOpenLocalSessionTplList } from '../../../../utils/hyopen'

export default {
  name: 'TableTplView',
  components: { inputTplUi, operatorBtnTplList },
  mixins: [resize],
  props: ['S_PAGE_ID', 'S_PAGE_VER', 'S_PARTITION', 'tableTplListUiData', 'tableEvent', 'tableId', 'paramData', 'tableObjParam', 'uiPageUrl', 'tableEchartId', 'appContainnerCssObj'],
  data() {
    return {
      appContainerCssClass: 'app-container',
      appDefaultContainerCssClass: 'app-container',
      appInnerContainerCssClass: 'app-inner-container',
      globalData: {},
      triggerOpenParam: {},
      searchText: '',
      searchInput: null,
      searchedColumn: '',
      initHeadTitle: false,
      initLoadObj: { loadFormId: '', loadBtnId: '', loadListId: '', loadPanelId: '', loadPanelVer: '' },
      initSrcObj: { initSrcUrl: 'page', initSrcLabel: 'normal', initSrcSearch: true },
      initReturnSetFormData: {},
      initReturnSetListData: {},
      uiParamData: {},
      initOpenData: {},
      initFormKey: 'formList',
      initTableKey: 'tableTplList',
      initUiVal: {}, // 初始化满足条件当前呈现UI数据
      rules: { }, // 查询条件校验规则
      openParam: {}, // 开窗数据
      initData: {}, // 查询条件初始化数据
      remoteDataSet: {},
      openReturnData: {}, // 开窗返回参数
      clickRow: {}, // 点击事件记录
      validatorRules: {},
      viewChart: null,
      showChart: { defaultShow: true },
      ehUrlKey: {},
      inputStyle: 'width: 260px',
      screenWidthFix: 1.1,
      inputWidthFix: 0.75,
      nowBtnItem: {},
      fileData: {},
      collapseKey: {},
      formList: { },
      formKey: [],
      modalFn: {},
      loading: false,
      formModel: {},
      formEvent: {},
      dealApi: {
        page: { S_PAGE_ID: '', S_PAGE_VER: 'v0.02', S_PARTITION: 32 },
        list: { S_PAGE_ID: '', S_PAGE_VER: 'v0.02', S_PARTITION: 32 }
      },
      dealParam: {
        page: { S_DY_PARAM: [] },
        delete: { S_DY_PARAM: [] }
      },
      activeEnumData: {},
      activeFilters: [],
      columnFiltersData: {},
      activeColumnFiltersData: {},
      formRowCollapseKey: {},
      formRowActiveData: {},
      formColActiveData: {},
      listColActiveList: [],
      listColActiveData: {}
    }
  },
  watch: {
    // listColActiveList(val) {
    //   console.log('0000000000listColActiveList0000000000',val)
    // },
    footerToolbarList(val) {
      this.$emit('triggerFooterToolbarData', val)
    },
    paramData(val) {
      const _this = this
      // this.tableOnLoadData();
      _this.initFormObjValue(val)
      _this.$nextTick(() => {
        console.debug('watch 数据变化 val ', val)
        _this.searchQuery()
      })
    },
    tableFirstRow(val) {
      // this.processCustomRowOnClick(val, 0)
    }
  },
  mounted() {
    // 延迟加载采用mounted重新挂载
    // if (undefined === this.tableTplListUiData) {
    //   return
    // }
    // this.initUI(this.tableTplListUiData, 'detail')
    if (undefined === this.paramData) {
      return
    }
    if (Object.keys(this.paramData).length !== 0) {
      console.debug('mount 数据变化 val ', this.paramData)
      // this.tableOnLoadData()
    }
  },
  methods: {
    changeAppContainnerCssObj() {
      this.appContainerCssClass = this.appInnerContainerCssClass
    },
    changeInitSrcObj(newInitSrcObj) {
      this.initSrcObj.initSrcSearch = false
      // this.initSrcObj = newInitSrcObj
    },
    setInitReturnSetFormData(initReturnSetFormData) {
      this.initReturnSetFormData = initReturnSetFormData
    },

    initEnumParam() {
      const thisObj = this
      initEnumParam(thisObj)
    },
    getLocalParamDataSet() {
      // 入参数据集
      const thisObj = this
      const record = Object.assign({}, thisObj.openParam, thisObj.initData, thisObj.openReturnData, thisObj.formModel)
      return record
    },
    changePanelInitUiVal(formItem, triggerObj) {
      // 变更呈现数据
      const selectedRow = this.tableSelectedRow
      const localParamDataSet = this.getLocalParamDataSet()
      this.$emit('tableChangeEvent', formItem, triggerObj, localParamDataSet, selectedRow)
    },
    changeUiEventTrigger(formItem, triggerObj) {
      const selectedRow = this.tableSelectedRow
      const localParamDataSet = this.getLocalParamDataSet()
      this.$emit('uiChangeEvent', formItem, triggerObj, localParamDataSet, selectedRow)
    },
    initForm() {
      const thisObj = this
      initForm(thisObj)
    },
    changeForm(triggerObj, param) {
      const thisObj = this
      changeForm(thisObj, triggerObj, param)
    },
    otherCompSetValClearData() {
      const thisObj = this
      const tmpData = Object.assign({})
      thisObj.tableQueryParam = Object.assign(tmpData)
      thisObj.initData = Object.assign(tmpData)
      thisObj.openReturnData = Object.assign(tmpData)
      thisObj.formModel = Object.assign(tmpData)
      thisObj.globalData = Object.assign(tmpData)
    },
    otherCompSetValOpenParam(triggerObj, param, initSrcObj) {
      const thisObj = this
      this.otherCompSetValClearData()
      this.initSrcObj = initSrcObj
      this.openParam = Object.assign({}, param)
      thisObj.formModel = Object.assign({}, this.openParam, param)
      // const record = thisObj.getLocalParamDataSet()

      const tmpGlobalData = {}
      tmpGlobalData.tableId = thisObj.tableId
      tmpGlobalData.initSrcObj = thisObj.initSrcObj
      tmpGlobalData.clickRow = {}
      tmpGlobalData.clickItem = {}
      tmpGlobalData.openParam = param
      tmpGlobalData.formModel = thisObj.formModel
      tmpGlobalData.initData = thisObj.initData
      tmpGlobalData.activeEnumData = thisObj.initData
      tmpGlobalData.tableSelectedRow = []
      tmpGlobalData.globalParam = getGlobalParam(thisObj)
      thisObj.globalData = tmpGlobalData
      if (thisObj.initSrcObj.initSrcSearch === undefined || thisObj.initSrcObj.initSrcSearch === true) {
        thisObj.triggerInitBtn()
      }
    },
    initRules() {
      // 开始初始化所有项目为必填
      const thisObj = this
      initRules(thisObj)
    },
    changeFormRules() {
      // 加载结束后,根据规则调整对应表单校验方式
      const thisObj = this
      changeFormRules(thisObj)
    },
    changeFormKeyRules(key) {
      // 单个变量校验规则调整设置
      const thisObj = this
      changeFormKeyRules(thisObj, key)
    },
    initFormFnParam() {
      // 一开始初始化读写参数
      const thisObj = this
      initFormFnParam(thisObj, true)
    },
    changeFormFnParam() {
      // 设置初始加载表单读写参数
      const thisObj = this
      changeFormFnParam(thisObj)
    },
    changeFormKeyFnParam(key) {
      const thisObj = this
      changeFormKeyFnParam(thisObj, key)
    },
    initTblEvent() {
      const thisObj = this
      initTableEvent(thisObj)
    },
    clearIniUi() {
      // 清除初始化数据
      const tmpArr = Object.assign([])
      const tmpData = Object.assign({})
      this.initUiVal = Object.assign(tmpData)
      this.initData = Object.assign(tmpData)
      this.nowRow = Object.assign(tmpData)
      this.openParam = Object.assign(tmpData)
      this.formModel = Object.assign(tmpData)
      this.formList = Object.assign(tmpData)
      this.activeEnumData = Object.assign(tmpData)
      this.tableToolbarMenu = Object.assign(tmpArr)
      this.tableToolbarList = Object.assign(tmpArr)
    },
    initUI(val, param, initSrcObj) {
      const thisObj = this
      this.initSrcObj = initSrcObj
      this.openParam = param
      this.formModel = Object.assign({}, this.openParam)
      this.initLoadObj.loadPanelId = val.panel.panelTplId
      this.initLoadObj.loadPanelVer = val.panel.panelTplVer
      const tmpGlobalData = {}
      tmpGlobalData.tableId = thisObj.tableId
      tmpGlobalData.initSrcObj = thisObj.initSrcObj
      tmpGlobalData.clickRow = {}
      tmpGlobalData.clickItem = {}
      tmpGlobalData.openParam = param
      tmpGlobalData.formModel = thisObj.formModel
      tmpGlobalData.initData = thisObj.initData
      tmpGlobalData.tableSelectedRow = []
      tmpGlobalData.globalParam = getGlobalParam(thisObj)
      thisObj.globalData = tmpGlobalData
      const tmpVal = Object.assign({}, val)
      this.clearIniUi()
      this.initUiVal = Object.assign({}, tmpVal)

      this.initEnumParam()
      this.initRules()
      this.initForm()
      this.initFormFnParam()
      initFooterToolbarList(this)
      if (undefined === val || undefined === val.urlList) {
        return
      }
      initDealApi(this, val)
      if (undefined !== val.style) {
        this.initTableStyle = val.style
      }
      if (this.initUiVal.sessionData !== undefined && this.initUiVal.sessionData.sessionConfigTpl !== undefined) {
        this.sessionConfigTplData = this.initUiVal.sessionData.sessionConfigTpl
      }
      thisObj.$nextTick(() => {
        thisObj.changeFormRules()
        thisObj.changeFormFnParam()
        handleInit(thisObj)
      })
    },
    triggerInitBtn() {
      const thisObj = this
      if (thisObj.initUiVal.toolbar !== undefined &&
        thisObj.initUiVal.toolbar.initBtnKey !== undefined &&
        thisObj.initUiVal.toolbar.initBtnKey.length > 0
      ) {
        const initBtnKey = thisObj.initUiVal.toolbar.initBtnKey
        for (const i in initBtnKey) {
          const btnKey = initBtnKey[i]
          console.log('触发初始化', btnKey)
          execClickBtn(thisObj, thisObj.globalData, btnKey)
        }
      } else {
        handleInit(thisObj)
      }
    },
    clearEmpty(item) {
      console.log('00000000000000', item)
    },
    inputChangeEventControlObj(formItem) {
      const thisObj = this
      const record = Object.assign({}, thisObj.initData, thisObj.openParam, thisObj.formModel)
      inputChangeEventControlObj(thisObj, formItem, record)
    },
    changeUploadFile(btnItem) {
      this.nowBtnItem = btnItem
    },
    importFileData(btnItem, data) {
      console.log('点击导入文件事件.', btnItem)
    },
    tableOperatorBtnUpload(btnItem, data) {
      console.log('点击导入文件事件.上传', btnItem)
      const thisObj = this
      btnUploadFile(thisObj, btnItem, data)
    },
    inputChangeEvent(formItem, data) {
      const thisObj = this
      inputChangeEvent(thisObj, formItem)
    },
    inputOpenGeneralModal(formItem, record) {
      console.log('...', formItem)
      this.$emit('openGeneralModal', formItem, record)
    },
    getCallFnByFnType(btnItem) {
      this.nowBtnItem = btnItem
      return getCallFnByFnType(btnItem)
    },
    executeBatchRemoteDelete(btnItem, globalData) {
      deleteBatchRemoteRecord(this, globalData)
    },
    executeRemoteDelete(btnItem, globalData) {
      // 本地删除
      deleteRemoteRecord(this, globalData)
    },
    executeBatchLocalDelete(btnItem, globalData) {
      deleteBatchLocalList(this, globalData)
    },
    executeLocalDelete(btnItem, globalData) {
      // 本地删除
      deleteLocalList(this, globalData)
    },
    executeTmpSave(btnItem, globalData) {
      // 暂存
    },
    btnClickEvent(btnItem, record) {
      const thisObj = this
      console.log('btnClickEvent', record)
      if (btnItem.fnType === 97) {
        this.saveSessionFormData(btnItem, record)
      } else if (btnItem.fnType === 92) {
        thisObj.openLocalSessionTplList(btnItem, thisObj.globalData)
      } else if (btnItem.fnType === 93) {
        thisObj.viewSessionFormDataList(btnItem, thisObj.globalData)
      } else if (btnItem.fnType === 94 || btnItem.fnType === 95) {
        // this.searchInitData(btnItem, record)
        searchSessionFormData(thisObj, thisObj.globalData)
        // }
        // else if (btnItem.fnType === 95) {
        //   console.log('00000000')
        // this.searchSessionFormData(btnItem, record)
      } else if (btnItem.fnType === 96) {
        searchSessionTpl(thisObj, thisObj.globalData)
      } else {
        this.$emit('btnClickEvent', btnItem, record)
      }
    },
    viewSessionFormDataList(btnItem, record) {
      const refOpenObj = getOpenLocalFormList()
      btnItem.refOpen = refOpenObj
      const localFormData = {}
      localFormData.S_PAGE_ID_IDX = this.uiPageUrl.S_UI_PAGE_ID_IDX_2025
      localFormData.S_PAGE_VER_IDX = this.uiPageUrl.S_UI_PAGE_VER_IDX_2025
      localFormData.S_PANEL_TPL_ID = this.initLoadObj.loadPanelId
      localFormData.S_PANEL_TPL_VER = this.initLoadObj.loadPanelVer
      const tmpGlobalData = {}
      tmpGlobalData.globalParam = localFormData
      tmpGlobalData.openParam = localFormData
      this.$emit(this.tableEvent.generalModal, btnItem, tmpGlobalData)
    },
    openLocalSessionTplList(btnItem, record) {
      const refOpenObj = getOpenLocalSessionTplList()
      btnItem.refOpen = refOpenObj
      const localFormData = {}
      localFormData.S_PAGE_ID_IDX = this.uiPageUrl.S_UI_PAGE_ID_IDX_2025
      localFormData.S_PAGE_VER_IDX = this.uiPageUrl.S_UI_PAGE_VER_IDX_2025
      localFormData.S_PANEL_TPL_ID = this.initLoadObj.loadPanelId
      localFormData.S_PANEL_TPL_VER = this.initLoadObj.loadPanelVer
      localFormData.S_FORM_BLOCK_TPL_ID = this.initLoadObj.loadFormId
      localFormData.S_LIST_BLOCK_TPL_ID = this.initLoadObj.loadListId
      localFormData.S_BTN_BLOCK_TPL_ID = this.initLoadObj.loadBtnId
      localFormData.openUi = 'tpl'
      const tmpGlobalData = {}
      tmpGlobalData.globalParam = localFormData
      tmpGlobalData.openParam = localFormData
      this.$emit(this.tableEvent.generalModal, btnItem, tmpGlobalData)
    },
    saveSessionFormData(btnItem, record) {
      const dataCompKey = this.initUiVal.formList.dataCompKey
      const tplCompKey = this.initUiVal.formList.tplCompKey
      const refOpenObj = getOpenLocalFormModel()
      btnItem.refOpen = refOpenObj
      this.formModel.S_FORM_DATA = undefined
      this.formModel[dataCompKey] = undefined
      const S_FORM_DATA = JSON.stringify(this.formModel)
      const localFormData = {}
      localFormData.S_FORM_DATA = S_FORM_DATA
      localFormData.S_PAGE_ID_IDX = this.uiPageUrl.S_UI_PAGE_ID_IDX_2025
      localFormData.S_PAGE_VER_IDX = this.uiPageUrl.S_UI_PAGE_VER_IDX_2025
      localFormData.S_BLOCK_TPL_ID = this.initLoadObj.loadFormId
      localFormData.S_PANEL_TPL_ID = this.initLoadObj.loadPanelId
      localFormData.S_PANEL_TPL_VER = this.initLoadObj.loadPanelVer
      localFormData.I_SESSION_TPL_ID = this.formModel[tplCompKey]
      const tmpGlobalData = {}
      tmpGlobalData.globalParam = localFormData
      tmpGlobalData.openParam = localFormData
      this.$emit(this.tableEvent.generalModal, btnItem, tmpGlobalData)
    },
    searchSessionTplData(btnItem, record) {
      const thisObj = this
      searchSessionFormData(thisObj, thisObj.globalData)
    },
    searchSessionFormData(btnItem, record) {
      const thisObj = this
      searchSessionFormData(thisObj, thisObj.globalData)
    },
    beforeInitData(btnItem, record) {
      const thisObj = this
      beforeInitData(thisObj, thisObj.globalData)
    },
    searchInitData(btnItem, record) {
      const thisObj = this
      beforeInitData(thisObj, thisObj.globalData)
    },
    openGeneralModal(btnItem, globalData) {
      const refOpenObj = getRefOpenObj(this, btnItem, globalData)
      if (refOpenObj !== null) {
        btnItem.refOpen = Object.assign({}, refOpenObj)
        // if (this.tableEvent.generalModal !== '') {
        this.$emit(this.tableEvent.generalModal, btnItem, globalData)
        // }
      } else {
        this.$message.warning('无满足条件开窗数据,请联系管理员.')
      }
    },
    getInputSetVal(record, btnItem) {
      return getFnInputParam(record, btnItem.refSetOpenVal)
    },
    downloadUploadFile(btnItem, record) {
      // 下载上传文件
      const inputParam = this.getInputSetVal(record, btnItem)
      const url = '/autodeal/file/ctr/download/' + inputParam.S_ID
      console.log('00downloadUploadFile000', inputParam)
      let downParam = {}
      if (btnItem.refSetOpenVal !== undefined) {
        downParam = inputParam
      }
      request.download(url, downParam, inputParam.S_FILE_NAME)
    },

    openSessionGeneralModal(btnItem, record) {
      const sessionRecord = Object.assign({}, this.initLoadObj, this.uiPageUrl)
      sessionRecord.I_APP_ID = I_APP_ID
      const thisObj = this
      thisObj.globalData.clickSrc = 'sessionClick'
      thisObj.globalData.tableId = thisObj.tableId
      thisObj.globalData.initSrcObj = thisObj.initSrcObj
      thisObj.globalData.clickRow = record
      thisObj.globalData.clickItem = {}
      thisObj.globalData.openParam = sessionRecord
      thisObj.globalData.formModel = thisObj.formModel
      thisObj.globalData.initData = thisObj.initData
      thisObj.globalData.initLoadObj = thisObj.initLoadObj
      thisObj.globalData.globalParam = getGlobalParam(thisObj)
      this.openGeneralModal(btnItem, thisObj.globalData)
    },
    executeLocalEvent(btnItem, record) {
      const eventKey = btnItem.key
      const eventList = this.initUiVal.toolbar.btnEvent[eventKey]
      if (!(undefined === eventList || eventList.length === 0)) {
        const newBtnItem = {}
        newBtnItem.operateEvent = 'click'
        newBtnItem.name = btnItem.name
        newBtnItem.eventList = eventList
        inputChangeEventControlObj(this, newBtnItem, record)
      }
    },
    execute(btnItem, record) {
      executeRecord(this, btnItem, record)
    }
  }
}
</script>
<style>
.el-dropdown-link {
  cursor: pointer;
  color: #409EFF;
}
.el-icon-arrow-down {
  font-size: 12px;
}
.highlight {
  background-color: rgb(255, 192, 105);
  padding: 0px;
}
</style>
